package com.yzion

import android.content.Intent
import android.os.Bundle
import android.provider.ContactsContract
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.*
import com.blankj.utilcode.util.BusUtils.Bus
import com.blankj.utilcode.util.PermissionUtils.FullCallback
import com.github.shadowsocks.bg.BaseService
import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager
import com.github.shadowsocks.preference.DataStore
import com.yzion.baseapp.R
import com.yzion.baseapp.databinding.MainActivityBinding
import com.yzion.baselib.activity.BaseActivity
import com.yzion.baselib.activity.BaseBindingActivity
import com.yzion.baselib.utils.onSingleClick
import com.yzion.baselib.widget.titlebar.DefaultTitleBar
import com.yzion.demo_coollist.CoolListSimpleActivity
import com.yzion.demo_navigationbar.DemoListActivity
import com.yzion.demo_refreshlayout.RefreshDemoActivity
import com.yzion.demo_refreshlayout.SuperTitleBar
import com.yzion.demo_swipeback.SwipeDemoActivity
import com.yzion.demo_tablayout.SimpleHomeActivity
import com.yzion.demo_webview.WebViewDemoActivity
import com.yzion.util.VPNController
import me.yokeyword.sample.EnterActivity

/**
 * 入口界面
 */
class MainActivity : BaseBindingActivity<MainActivityBinding>() {

//    public override fun getLayoutId(): Int {
//        return R.layout.main_activity
//    }

    override fun isEnableImmersive(): Boolean {
        return true
    }

    override fun isUseDefaultTitleBar(): Boolean {
        return true
    }

    override fun initView() {
        getTitleBar<DefaultTitleBar>()?.setTitleText("HotYzion")

        binding.btnNetlistDemo.setOnClickListener {
            startActivity(
                Intent(context, CoolListSimpleActivity::class.java)
            )
        }
        binding.btnSwipeDemo.setOnClickListener {
            ActivityUtils.startActivity(
                Intent(context, SwipeDemoActivity::class.java)
            )
        }
        binding.btnNavigationbarDemo.setOnClickListener {
            ActivityUtils.startActivity(
                Intent(context, DemoListActivity::class.java)
            )
        }
        binding.btnFragmentDemo.setOnClickListener {
            ActivityUtils.startActivity(
                Intent(context, EnterActivity::class.java)
            )
        }
        binding.btnRefreshDemo.setOnClickListener {
            ActivityUtils.startActivity(
                Intent(context, RefreshDemoActivity::class.java)
            )
        }
        binding.btnTablyaoutDemo.setOnClickListener {
            startActivity(
                Intent(context, SimpleHomeActivity::class.java)
            )
        }
        binding.btnWebviewDemo.setOnClickListener {
            startActivity(
                Intent(context, WebViewDemoActivity::class.java)
            )
        }

        //调试vpn的
        binding.btnStartVpn.onSingleClick {
            binding.btnStartVpn.isEnabled = false
            DataStore.profileId = ProfileManager.createProfile(Profile().apply {
                name = "Peiji"
                host = "44.203.57.187"
                password = "be4TYhVx7H"
                remotePort = 443
                method = "aes-256-gcm"
            }).id

            VPNController.connect()
        }

        binding.btnStopVpn.onSingleClick {
            VPNController.stop()
        }

        VPNController.stateCallback = {
            LogUtils.d(it.name)
            when (it) {
                BaseService.State.Idle -> {
                    binding.btnStartVpn.text = "Idle"
                    binding.btnStartVpn.isEnabled = true
                }
                BaseService.State.Connecting -> {
                    binding.btnStartVpn.text = "Connecting"
                }
                BaseService.State.Connected -> {
                    binding.btnStartVpn.text = "Connected"
                    binding.btnStartVpn.isEnabled = true
                }
                BaseService.State.Stopping -> {
                    binding.btnStartVpn.text = "Stopping"
                }
                BaseService.State.Stopped -> {
                    binding.btnStartVpn.text = "Stopped"
                    binding.btnStartVpn.isEnabled = true
                }
            }
        }

        ThreadUtils.runOnUiThreadDelayed({ showLoading("I Love U...") }, 1000)
        ThreadUtils.runOnUiThreadDelayed({ hideLoading() }, 2000)
    }

    private fun requestPermission() {
        PermissionUtils.permission(PermissionConstants.PHONE, PermissionConstants.STORAGE)
            .callback(object : FullCallback {
                override fun onGranted(permissionsGranted: List<String>) {}
                override fun onDenied(
                    permissionsDeniedForever: List<String>,
                    permissionsDenied: List<String>
                ) {
                }
            }).request()
    }

    override fun initData() {
        requestPermission()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    @Bus(tag = BUS_MAIN_ACTIVITY_ON_EVENT)
    fun onEvent() {
        ToastUtils.showShort("BUS_MAIN_ACTIVITY_ON_EVENT")
    }

    companion object {
        private const val BUS_MAIN_ACTIVITY_ON_EVENT = "BUS_MAIN_ACTIVITY_ON_EVENT"
    }

//    override fun getViewBinding(view: View): MainActivityBinding {
//        return MainActivityBinding.bind(view)
//    }

    override fun getViewBinding(inflater: LayoutInflater): MainActivityBinding {
        return MainActivityBinding.inflate(inflater)
    }

    override fun initIntent() {

    }
}